package com.my.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.my.R;
import com.my.adapter.AskLeaveAdapter;
import com.my.adapter.entity.AskLeave;
import com.my.util.ApiUtil;

import java.util.ArrayList;
import java.util.List;

public class AskLeaveListActivity extends AppCompatActivity {

    ListView listView;
    JSONObject user = null;

    Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            JSONObject rest = ((JSONObject) msg.obj);
            if(rest.getInt("code") == 200) {
                List<JSONObject> list = rest.getJSONObject("data").getJSONArray("list").toList(JSONObject.class);
                listView.setAdapter(new AskLeaveAdapter(AskLeaveListActivity.this, R.layout.adapter_ask_leave, list));
            }else {
                Toast.makeText(AskLeaveListActivity.this, "服务器异常！请稍后再试！", Toast.LENGTH_LONG).show();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ask_leave_list);
        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowCustomEnabled(true);
        }
        listView = findViewById(R.id.list);
        SharedPreferences preferences = this.getSharedPreferences("LOGIN_DATA", Context.MODE_PRIVATE);
        user = new JSONObject(preferences.getString("user", null));
        new Thread(() -> {
            Message message = new Message();
            String body = HttpRequest.get(ApiUtil.HTTP_ADDRESS + "askLeaveById?" + "userId=" + user.getLong("id")).execute().body();
            message.obj = new JSONObject(body);
            handler.sendMessage(message);
        }).start();
        listView.setOnItemClickListener((leaves, view, pos, v4) -> {
            Intent intent = new Intent(this, AskLeaveReviewActivity.class);
            intent.putExtra("item", leaves.getItemAtPosition(pos).toString());
            startActivity(intent);
        });
    }

    @Override
    protected void onRestart() {
        new Thread(() -> {
            Message message = new Message();
            String body = HttpRequest.get(ApiUtil.HTTP_ADDRESS + "askLeaveById?" + "userId=" + user.getLong("id")).execute().body();
            message.obj = new JSONObject(body);
            handler.sendMessage(message);
        }).start();
        super.onRestart();
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == 16908332) {
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }
}